﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nova.Parsing.GoldParser.Semantic;
using Nova.Parsing.Tokens;

namespace Nova.Parsing.Constructs
{
    public class ArgumentSetConstruction : NovaConstruct, IEnumerable<ExpressionConstruct>
    {
        private ExpressionConstruct m_Value;
        private ArgumentSetConstruction m_NextArgumentSet;

        [Rule(@"<Argument List> ::= <Expression> ',' <Argument List>")]
        [Rule(@"<Argument List> ::= <Expression>")]
        public ArgumentSetConstruction(ExpressionConstruct value, IgnoredToken iA, ArgumentSetConstruction argSet)
        {
            m_Value = value;
            m_NextArgumentSet = argSet;
        }

        [Rule(@"<Argument List Opt> ::= <Argument List>")]
        [Rule(@"<Argument List Opt> ::= ")]
        public ArgumentSetConstruction(ArgumentSetConstruction argSet)
        {
            m_NextArgumentSet = argSet;
            m_Value = null;
        }

        public ExpressionConstruct Value
        {
            get { return m_Value; }
        }

        public ArgumentSetConstruction NextArgumentSet
        {
            get { return m_NextArgumentSet; }
        }

        public override string ToString()
        {
            StringBuilder argumentList = new StringBuilder();

            foreach (ExpressionConstruct value in this)
            {
                argumentList.Append(value.ToString() + ", ");
            }

            if (argumentList.Length > 0)
                argumentList.Remove(argumentList.Length - 2, 2);

            return argumentList.ToString();
        }

        public IEnumerator<ExpressionConstruct> GetEnumerator()
        {
            for (ArgumentSetConstruction set = this; set != null; set = set.NextArgumentSet)
            {
                if (set.Value != null)
                {
                    yield return set.Value;
                }
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
